package com.pub.paypal.service.impl;


import com.pub.paypal.common.operation.CaptureOrder;
import com.pub.paypal.common.operation.RefundOrder;
import com.pub.paypal.common.vo.PaypalVo;
import com.pub.paypal.model.RefundEntity;
import com.pub.paypal.service.PaypalService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * paypal支付接口实现
 */
@Service
@Slf4j
@AllArgsConstructor
public class PaypalServiceImpl implements PaypalService {

    @Autowired
    private CaptureOrder captureOrder;

    @Autowired
    private RefundOrder refundOrder;

    /**
     * 执行支付 捕获订单进行扣款操作
     * @param token paypal支付编号id 唯一
     * @param payerID
     * @return
     * @throws
     */
    @Override
    public PaypalVo successPay(String token, String payerID) {
        PaypalVo result = null;
        try {
            result = captureOrder.captureOrder(token);  //执行扣款
            captureOrder.getCapture(result.getCaptureId());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String refund(RefundEntity refundEntity) {
        String result;
        try {
            result = refundOrder.refundOrder(refundEntity);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public String getRefund(String refundId) {
        String result = "";
        try {
            result =  refundOrder.get(refundId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}